<?php
// 本类由系统自动生成，仅供测试用途
class LevelAction extends PublicAction {
    //首页
	public function index(){
	  $isLogin = session(C('ADMIN_AUTH_KEY'));
	  if(!$isLogin) $this->redirect('User/login');
	  $level=M('Level');
	  $list = $level->order('lmins asc')->select();
	  $this->assign("list",$list);
	  $this->display();
	}
 public function addsource(){
   $isLogin = session(C('ADMIN_AUTH_KEY'));
   if(!$isLogin) $this->redirect('User/login');
   if($_POST){
   	 import("ORG.Util.UploadFile");
        //导入上传类
        $upload = new UploadFile();
        //设置上传文件大小
        $upload->maxSize = 3292200;
        //设置上传文件规则
        $upload->saveRule = uniqid;
        //设置上传文件类型
        $upload->allowExts = explode(',', 'bmp,jpg,gif,png,jpeg');
        //设置附件上传目录
        $upload->savePath = './Public/Admin/Uploads/';
        if (!$upload->upload($_FILES)) {
             echo "<font color='red>上传失败</font>";
            //捕获上传异常
            $this->error($upload->getErrorMsg());
        } else {
            //取得成功上传的文件信息
            $uploadList = $upload->getUploadFileInfo();
        }
   	     $_POST['limage']=$uploadList[0]['savename'];
        $data=$_POST; 
		$level=D('Level');
		if($level->create($data)){
			if($level->add()!=false){
			  $this->redirect('Level/index');
			}
		}else{
			  unlink("./Public/Admin/Uploads/".$data['limage']);
			  $this->assign('erorsta','1');
			  $this->assign('error',$level->getError());
		}
    }
   $this->display();
   }
   
   public function edit(){
   $isLogin = session(C('ADMIN_AUTH_KEY'));
   if(!$isLogin) $this->redirect('User/login');
    $fid['lid']=$_GET['lid'];
    $model=D('Level');
    $data=$model->where($fid)->find();
    $this->assign('data',$data);
    if($_POST){
    	if(!empty($_FILES['limage']['name'])){
    	import("ORG.Util.UploadFile");
        //导入上传类
        $upload = new UploadFile();
        //设置上传文件大小
        $upload->maxSize = 3292200;
        //设置上传文件规则
        $upload->saveRule = uniqid;
        //设置上传文件类型
        $upload->allowExts = explode(',', 'bmp,jpg,gif,png,jpeg');
        //设置附件上传目录
        $upload->savePath = './Public/Admin/Uploads/';
        if (!$upload->upload($_FILES)) {
            //捕获上传异常
            $this->error($upload->getErrorMsg());
        } else {
        	unlink('./Public/Admin/Uploads/'.$data['limage']);
            //取得成功上传的文件信息
            $uploadList = $upload->getUploadFileInfo();
        }
   	     $_POST['limage']=$uploadList[0]['savename'];
    	}
    	$info=$_POST;
	    $info['lid']=$_GET['lid'];
	    if($model->create($info)){
	   	 $model->save();
	   	 $this->redirect('Level/index');
	   	}else{    unlink("./Public/Admin/Uploads/".$info['limage']);
				  $this->assign('erorsta','1');
				  $this->assign('error',$model->getError());
			}
    }

    $this->display();
   }
   
  public function move(){
  $isLogin = session(C('ADMIN_AUTH_KEY'));
  if(!$isLogin) $this->redirect('User/login');
    if($_GET['lid']==false){
    	$this->assign('erorsta','1');
		$this->assign('error',"未获取到id");
		$this->display('Level/index');
    }
    $fid['lid']=$_GET['lid'];
    $model=D('Level');
    $name=$model->field('limage')->where($fid)->find();
    if($model->create($fid)){
    $model->delete();
    unlink('./Public/Admin/Uploads/'.$name['limage']);
    $this->redirect('Level/index');
    }else{
				  $this->assign('erorsta','1');
				  $this->assign('error',$model->getError());
				  $this->display('Level/index');
			}
   }
 }